import { Tag } from 'antd';
import type { ProColumns } from '@ant-design/pro-table';
import { crud } from './api';
import BasicForm from './form';
import BasicFormTable from '@/components/BasicFormTable';
import { ADMIN_GROUP_NAME } from '@/constants';

export default () => {
  const normalize = (data: AnyFields) => ({
    ...data,
    allow_nodes: data.nodes.join(','),
  });

  const columns: ProColumns<TenantEntity>[] = [
    {
      title: '名称',
      dataIndex: 'name',
    },
    {
      title: '节点',
      dataIndex: 'nodes',
      render: (_, record) => {
        const { nodes } = record;
        return nodes?.map((item) => <Tag key={item}>{item}</Tag>);
      },
    },
    {
      title: '最大CPU(核)',
      dataIndex: 'max_cpu',
    },
    {
      title: '最大内存(G)',
      dataIndex: 'max_memory',
    },
    {
      title: '最大硬盘(G)',
      dataIndex: 'max_disk',
    },
  ];

  return (
    <BasicFormTable<TenantEntity>
      title={'租户'}
      columns={columns}
      search={false}
      postData={(data) =>
        data.map((item) => ({
          ...item,
          nodes: item.allow_nodes.split(',') || [],
        }))
      }
      crud={{
        ...crud,
        add: (data) => crud.add(normalize(data)),
        update: (data) => crud.update(normalize(data)),
      }}
      addForm={<BasicForm />}
      updateForm={<BasicForm />}
      renderActions={(actions, record) => {
        if (record.name === ADMIN_GROUP_NAME) return actions.slice(0, 1);
        return actions;
      }}
    />
  );
};
